package com.cloud.filter;


import com.cloud.comm.CommonContext;
import com.cloud.constant.Constants;
import com.cloud.util.SysUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * @version v1.0
 * @author: TianXiang
 * @description: 初始化变量的过滤器
 * @date: 2020/10/22
 */
@WebFilter(urlPatterns = "/admin/*", filterName = "variableFilter")
public class VariableFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) {}

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        CommonContext context = CommonContext.getContext();
        String moviePath, ffmpegExePath;
        if(SysUtils.isLinux()) {
            if(Constants.GREEN_MODEL.equals(request.getParameter("model"))) {
                moviePath = "/usr/local/app/docker/nginx/data/html/greens";
            }else {
                moviePath = "/usr/local/app/docker/nginx/data/html/movies";
            }
            ffmpegExePath = "ffmpeg";
        }else {
            if(Constants.GREEN_MODEL.equals(request.getParameter("model"))) {
                moviePath = "C:\\Develop\\greens";
            }else {
                moviePath = "C:\\Develop\\movies";
            }
            ffmpegExePath = "C:\\Develop\\ffmpeg\\bin\\ffmpeg.exe";
        }
        context.setMoviesPath(moviePath);
        context.setFfmpegExePath(ffmpegExePath);
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {}
}
